package com.ghp.search.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ghp.search.exception.ThrowUtils;
import com.ghp.search.common.BaseResponse;
import com.ghp.search.common.ErrorCode;
import com.ghp.search.common.ResultUtils;
import com.ghp.search.model.dto.picture.PictureQueryRequest;
import com.ghp.search.model.entity.Picture;
import com.ghp.search.service.PictureService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author ghp
 * @date 2023/5/30
 * @title
 * @description
 */
@RestController
@RequestMapping("/picture")
public class PictureController {

    @Resource
    private PictureService pictureService;

    /**
     * 分页获取图片列表
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<Picture>> listPictureByPage(@RequestBody PictureQueryRequest pictureQueryRequest) {
        long current = pictureQueryRequest.getCurrent();
        long size = pictureQueryRequest.getPageSize();
        // 限制爬虫（每次获取的数据不能超过20条，防止过大数据请求，严重影响服务器的正常服务）
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        String searchText = pictureQueryRequest.getSearchText();
        Page<Picture> picturePage = pictureService.searchPicture(searchText, current, size);
        return ResultUtils.success(picturePage);
    }

}
